package com.muye.dianli.fragment;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.muye.adapter.Fragment0ViewPagerAdapter;
import com.muye.constant.C;
import com.muye.dianli.R;
import com.muye.dianli.activity.FollowDetailActivity;
import com.muye.field.HomePageListData;
import com.zvezda.android.utils.BaseUtils;
import com.zvezda.disply.utils.DisplayConfig;
import com.zvezda.disply.utils.DisplayManager;

/**
 * 主页热点文章碎片
 * 
 * @author TangChao
 *
 */
public class MainFragment0 extends Fragment implements OnClickListener
{
	private List<HomePageListData> arrayList;
	
	private List<Fragment0ViewPagerFragment> dataList;
	
	private ViewPager viewPager;
	
	private Fragment0ViewPagerAdapter adapter;
	
	private Handler handler = new Handler();
	
	private Runnable runnable;
	
	public List<HomePageListData> getArrayList()
	{
		return arrayList;
	}
	
	public void setArrayList(List<HomePageListData> arrayList)
	{
		this.arrayList = arrayList;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		File file = new File(C.FILE_PATH);
		if (!file.exists())
		{
			file.mkdirs();
		}
	}
	
	@Override
	public void onDestroy()
	{
		super.onDestroy();
		handler.removeCallbacks(runnable);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View view = inflater.inflate(R.layout.main_fragment0_layout, null);
		dataList = new ArrayList<Fragment0ViewPagerFragment>();
		for (int i = 0; i < 4; i++)
		{
			Fragment0ViewPagerFragment item = new Fragment0ViewPagerFragment();
			String str = "http://115.28.138.45/uploads/201402/530f3eb8a264d.jpg";
			item.setPicURL(str);
			dataList.add(item);
		}
		adapter = new Fragment0ViewPagerAdapter(getFragmentManager(), dataList);
		viewPager = (ViewPager) view.findViewById(R.id.fragment0_view_pager);
		runnable = new Runnable()
		{
			@Override
			public void run()
			{
				viewPager.setAdapter(adapter);
			}
		};
		handler.postDelayed(runnable, 100);
		/* ================== */
		ImageView imageView0 = (ImageView) view.findViewById(R.id.fragment0_image_view0);
		imageView0.setOnClickListener(this);
		TextView imageView1 = (TextView) view.findViewById(R.id.fragment0_image_view1);
		imageView1.setText("热点文章");
		imageView1.setOnClickListener(this);
		ImageView imageView2 = (ImageView) view.findViewById(R.id.fragment0_image_view2);
		imageView2.setOnClickListener(this);
		ImageView imageView3 = (ImageView) view.findViewById(R.id.fragment0_image_view3);
		imageView3.setOnClickListener(this);
		ImageView imageView4 = (ImageView) view.findViewById(R.id.fragment0_image_view4);
		imageView4.setOnClickListener(this);
		/* ===================== */
		DisplayManager.getInstance().open(getActivity());
		showPicure(0, 240, 120, imageView0);
		// showPicure(1, 120, 120, imageView1);
		showPicure(2, 120, 120, imageView2);
		showPicure(3, 120, 120, imageView3);
		showPicure(4, 120, 120, imageView4);
		return view;
	}
	
	/**
	 * 显示图片
	 */
	private void showPicure(int position, int w, int h, ImageView imageView)
	{
		String picUrl;
		try
		{
			picUrl = arrayList.get(position).logoUrl;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			picUrl = "http://115.28.138.45/uploads/201402/530f3eb8a264d.jpg";
		}
		DisplayConfig displayConfig = new DisplayConfig();
		displayConfig.path = BaseUtils.getFilePath(C.FILE_PATH, picUrl);
		displayConfig.url = picUrl;
		displayConfig.w = w;
		displayConfig.h = h;
		displayConfig.defaultRes = R.drawable.ic_launcher;
		displayConfig.imageView = imageView;
		DisplayManager.getInstance().display(displayConfig);
	}
	
	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
			case R.id.fragment0_image_view0:
				Intent intent0 = new Intent(getActivity(), FollowDetailActivity.class);
				startActivity(intent0);
				break;
			case R.id.fragment0_image_view1:
				Intent intent1 = new Intent(getActivity(), FollowDetailActivity.class);
				startActivity(intent1);
				break;
			case R.id.fragment0_image_view2:
				Intent intent2 = new Intent(getActivity(), FollowDetailActivity.class);
				startActivity(intent2);
				break;
			case R.id.fragment0_image_view3:
				Intent intent3 = new Intent(getActivity(), FollowDetailActivity.class);
				startActivity(intent3);
				break;
			case R.id.fragment0_image_view4:
				Intent intent4 = new Intent(getActivity(), FollowDetailActivity.class);
				startActivity(intent4);
				break;
			
			default:
				break;
		}
	}
}
